package sketch.specs.symbc;

import java.lang.reflect.Method;

import sketch.util.Checker;

public class MyClassReflection1 {

	public static void main(String[] args) {
		String className =  "sketch.specs.symbc.MyClass1";//args[0];
		String methodName = "myMethod";//args[1];
		
		Class<?> clazz = null;
		try {
			clazz = Class.forName(className);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
		
		Checker.checkNull(clazz, "Class : " + className + " does not exist!");
		Method m = null;
		
		for(Method method : clazz.getDeclaredMethods()) {
			if(method.getName().equals(methodName)) {
				m = method;
				break;
			}
		}
		Checker.checkNull(m, "Method: " + methodName + " does not exist in class: " + className);
		
		Object object = null;
		try {
			object = clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		Checker.checkNull(object, "Can not create an instance for class: " + className);
		
		Class<?>[] types = m.getParameterTypes();
		Object[] params = new Object[types.length];
		for(int i = 0; i < params.length; i++) {
			Checker.checkTrue(types[i].equals(int.class), "Only support int right now.");
			params[i] = 0;
		}
		
		//invoke it
		try {
			m.invoke(object, params);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
}